﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.AnalysisServices.AdomdClient;

namespace Ko2.DB.BI
{

    public class MXBase
    {

        #region Members

        protected Ko2.Portal.Env _env = new Ko2.Portal.Env();

        #endregion

        #region Common

        public static string MakeDT_Y_M(int Year, int Month)
        {
            return string.Format("{0:0000}{1:00}", Year, Month);
        }

        public static string MakeDT_D_M_Y(int Day, int Month, int Year )
        {
            return string.Format("{0:00}.{1:00}.{2:0000}", Day, Month, Year);
        }

        public string GetPrevMonth(DateTime dt, int iMinus)
        {
            string ret = "";
            int i = Math.Abs(iMinus);
            i = i * -1;
            dt = dt.AddMonths(i);
            ret = string.Format("{0:0000}{1:00}", dt.Year, dt.Month);
            return ret;
        }

        #endregion

        public Exception Exec(ref CellSet parCst, string parConnStr, string parCmdStr)
        {
            Exception ret = null;
            try
            {
                using (AdomdConnection conn = new AdomdConnection(parConnStr))
                {
                    conn.Open();
                    using (AdomdCommand cmd = new AdomdCommand(parCmdStr, conn))
                    {
                        parCst = cmd.ExecuteCellSet();
                        conn.Close();
                    }
                }
            }
            catch (Exception err)
            {
                parCst = null;
                ret = new Exception(err.Message, err);
            }

            return ret;
        }

    }

}
